<?php

namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller {

    //首页
    public function index() {
        //获取微信配置信息
        include(__LIBS__ . 'wxjssdk/jssdk.php');
        $area = I("area");
        $wchat = C('weixin');
        $appid = $wchat['Appid'];
        $secret = $wchat['AppSecret'];
        $jssdk = new JSSDK("$appid", "$secret");
        $signPackage = $jssdk->GetSignPackage();
        //获取科室
        $section = M("section_type")->select();
        //获取十条初始医生数据
        if (!empty($area)) {
            $map["address"] = array('like', '%' . $area . '%');
        }
        $res = M("doctor")->where($map)->limit(0, 10)->order("sort desc")->select();
        $this->assign("data", $res);
        $this->assign("section", $section);
        $this->assign('signPackage', $signPackage);
        $this->display('index');
    }

    //获取医生列表
    public function doctor_list() {
        $lat = I("lat");
        $lng = I("lng");
        $tid = I("tid");
        $p = I("p") ? I("p") : 1;
        $name = I("name");
        $area = I("area");
        if (!empty($area)) {
            $map["address"] = array('like', '%' . $area . '%');
        }
        if (!empty($tid)) {
            $map["tid"] = $tid;
        }
        // $lat = '34.7913';
        //$lng = '113.673';
        /* $range = 180 / pi() * 300 / 6372.797; //里面的 15 就代表搜索 15km 之内，单位km 

          $lngR = $range / cos($lat * pi() / 180);
          $maxLat = $lat + $range; //最大纬度
          $minLat = $lat - $range; //最小纬度
          $maxLng = $lng + $lngR; //最大经度
          $minLng = $lng - $lngR; //最小经度
          $map['lng'] = array('between', array($minLng, $maxLng)); //经度值
          $map['lat'] = array('between', array($minLat, $maxLat)); //纬度值 */
        $pagesize = 10;
        $listrow = ($p - 1) * $pagesize;
        if (!empty($name)) {
            $map = array();
            $map["d.name"] = array("like", "%" . $name . "%");
            $map["s.name"] = array("like", "%" . $name . "%");
            $map["d.intro"] = array("like", "%" . $name . "%");
            $data["search"] = $name;
            $map["_logic"] = "or";
            request_by_fsockopen(IMG_SERVER . U("index/search_add"), $data);
            $res = M("doctor")
                    ->alias("d")
                    ->field("d.*")
                    ->join("left join section_type as s on s.id=d.tid")
                    ->where($map)
                    ->limit($listrow, $pagesize)
                    ->order("d.sort desc")
                    ->select();
        } else {
            $res = M("doctor")->where($map)->limit($listrow, $pagesize)->order("sort desc")->select();
        }
        $this->ajaxReturn($res);
    }

    /**
     * 挂号预约
     */
    public function order() {
        $data['name'] = I("userId");
        $data['order_time'] = strtotime(I("datetimepicker"));
        $data['mobile'] = I("mobile");
        $where['code'] = I("yanzheng");
        $data['address'] = I("adrees");
        $data['did'] = I('did');
        $data['sex'] = I('sex');
        $data['age'] = I('age');
        $data['xiguan'] = I('xiguan');
        $data['created'] = time();
        $datas['status'] = 2;
        $where['status'] = 1;
        $where['mobile'] = I("mobile");
        $map["did"] = $data['did'];
        $reone = M('doctor')->field('reception_count')->where($map)->find();
        $dta = array(
            val => 0,
            str => "失败",
        );
        if ($data['name'] && $data['order_time'] && $data['mobile'] && $where['code'] && $data['did']) {
            $re = M("code")->where($where)->find();
            if ($re) {
                $time = strtotime("+5 minutes", $re['time']);
                if ($data['created'] > $time) {
                    $dta['str'] = "验证码失效";
                } else {
                    $res = M("orders")->add($data);
                    if (res) {
                        M('code')->where($where)->save($datas);
                        $count['reception_count'] = $reone['reception_count'] + 1;
                        M('doctor')->where($map)->save($count);
                        $dta['val'] = 1;
                        $dta['str'] = "预约成功 我们的客服电话是:0755-2660 6003 稍后我们的客服人员会和您取得联系";
                    } else {
                        $dta['str'] = "预约失败";
                    }
                }
            } else {
                $dta["str"] = "验证码错误";
            }
        } else {
            $dta['str'] = "请填写完整";
        }
        $this->ajaxReturn($dta);
    }

    /**
     * 热门搜索
     */
    public function hot_search() {
        $re = M('hot_search')->order("count desc")->limit(0, 15)->select();
        $this->ajaxReturn($re);
    }

    /**
     * 热搜词添加
     */
    public function search_add() {
        $search = I("search");
        if (!empty($search)) {
            $data["field"] = $search;
            $db = M('hot_search');
            $re = $db->where($data)->find();
            if ($re) {
                $map["count"] = $re["count"] + 1;
                $db->where($data)->save($map);
            } else {
                $db->add($data);
            }
        }
    }

    public function adress() {
         //获取微信配置信息
        include(__LIBS__ . 'wxjssdk/jssdk.php');
		$wchat = C('weixin');
        $appid = $wchat['Appid'];
        $secret = $wchat['AppSecret'];
        $jssdk = new JSSDK("$appid", "$secret");
        $signPackage = $jssdk->GetSignPackage();
		
        $area = M("address")->select();
        $data = array();
        foreach ($area as $v) {
            $k = substr($v["english"],0,1);
            $data[$k][] = $v;
        }
        ksort($data);
		$this->assign('signPackage', $signPackage);
        $this->assign("data", $data);
        $this->display();
    }

}
